package com.todo.server.DAO.type;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.todo.server.DAO.type.model.TypeDAO;
import com.todo.orm.type.Type;
import com.todo.util.HibernateUtil;

public class TypeDAOImpl extends RemoteServiceServlet implements TypeDAO{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5989373773048253330L;

	public Type getTypeById(int id) {
		Session session = null;
	    Type type = null;
	    try {
	      session = HibernateUtil.getSessionFactory().openSession();
	      type = (Type) session.load(Type.class, id);
	    } catch (Exception e) {
	    	System.err.println("Error --->" + e.getMessage());
	    } finally {
	      if (session != null && session.isOpen()) {
	        session.close();
	      }
	    }
	    return type;
	}

	public List<Type> getAllTypes() {
		Session session = null;
	    List<Type> types = new ArrayList<Type>();
	    try {
	      session = HibernateUtil.getSessionFactory().openSession();
	      types = session.createCriteria(Type.class).list();
	    } catch (Exception e) {
	    	System.err.println("Error --->" + e.getMessage());
	    } finally {
	      if (session != null && session.isOpen()) {
	        session.close();
	      }
	    }
	    return types;
	}

}
